﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Terraria.ModLoader.Exceptions;
using Terraria.ModLoader.IO;
using Terraria.Social;
using Terraria.Utilities;

namespace Terraria.IO
{
	// Token: 0x0200032C RID: 812
	public class PlayerFileData : FileData
	{
		// Token: 0x1700021A RID: 538
		// (get) Token: 0x06001E67 RID: 7783 RVA: 0x0001598C File Offset: 0x00013B8C
		// (set) Token: 0x06001E68 RID: 7784 RVA: 0x00015994 File Offset: 0x00013B94
		public Player Player
		{
			get
			{
				return this._player;
			}
			set
			{
				this._player = value;
				if (value != null)
				{
					this.Name = this._player.name;
				}
			}
		}

		// Token: 0x06001E69 RID: 7785 RVA: 0x000159B1 File Offset: 0x00013BB1
		public PlayerFileData() : base("Player")
		{
		}

		// Token: 0x06001E6A RID: 7786 RVA: 0x000159D4 File Offset: 0x00013BD4
		public PlayerFileData(string path, bool cloudSave) : base("Player", path, cloudSave)
		{
		}

		// Token: 0x06001E6B RID: 7787 RVA: 0x00441D2C File Offset: 0x0043FF2C
		public static PlayerFileData CreateAndSave(Player player)
		{
			PlayerFileData playerFileData = new PlayerFileData();
			playerFileData.Metadata = FileMetadata.FromCurrentSettings(FileType.Player);
			playerFileData.Player = player;
			playerFileData._isCloudSave = (SocialAPI.Cloud != null && SocialAPI.Cloud.EnabledByDefault);
			playerFileData._path = Main.GetPlayerPathFromName(player.name, playerFileData.IsCloudSave);
			(playerFileData.IsCloudSave ? Main.CloudFavoritesData : Main.LocalFavoriteData).ClearEntry(playerFileData);
			Player.SavePlayer(playerFileData, true);
			return playerFileData;
		}

		// Token: 0x06001E6C RID: 7788 RVA: 0x000159F9 File Offset: 0x00013BF9
		public override void SetAsActive()
		{
			Main.ActivePlayerFileData = this;
			Main.player[Main.myPlayer] = this.Player;
		}

		// Token: 0x06001E6D RID: 7789 RVA: 0x00441DA8 File Offset: 0x0043FFA8
		public override void MoveToCloud()
		{
			if (base.IsCloudSave || SocialAPI.Cloud == null)
			{
				return;
			}
			string playerPathFromName = Main.GetPlayerPathFromName(this.Name, true);
			if (FileUtilities.MoveToCloud(base.Path, playerPathFromName))
			{
				PlayerIO.MoveToCloud(base.Path, playerPathFromName);
				string fileName = base.GetFileName(false);
				string path = string.Concat(new object[]
				{
					Main.PlayerPath,
					System.IO.Path.DirectorySeparatorChar,
					fileName,
					System.IO.Path.DirectorySeparatorChar
				});
				if (Directory.Exists(path))
				{
					string[] files = Directory.GetFiles(path);
					for (int i = 0; i < files.Length; i++)
					{
						string cloudPath = string.Concat(new string[]
						{
							Main.CloudPlayerPath,
							"/",
							fileName,
							"/",
							FileUtilities.GetFileName(files[i], true)
						});
						FileUtilities.MoveToCloud(files[i], cloudPath);
					}
				}
				Main.LocalFavoriteData.ClearEntry(this);
				this._isCloudSave = true;
				this._path = playerPathFromName;
				Main.CloudFavoritesData.SaveFavorite(this);
			}
		}

		// Token: 0x06001E6E RID: 7790 RVA: 0x00441EB0 File Offset: 0x004400B0
		public override void MoveToLocal()
		{
			if (!base.IsCloudSave || SocialAPI.Cloud == null)
			{
				return;
			}
			string playerPathFromName = Main.GetPlayerPathFromName(this.Name, false);
			if (FileUtilities.MoveToLocal(base.Path, playerPathFromName))
			{
				PlayerIO.MoveToLocal(base.Path, playerPathFromName);
				string fileName = base.GetFileName(false);
				string mapPath = System.IO.Path.Combine(Main.CloudPlayerPath, fileName);
				IEnumerable<string> enumerable = from path in SocialAPI.Cloud.GetFiles()
				where path.StartsWith(mapPath, StringComparison.CurrentCultureIgnoreCase) && (path.EndsWith(".map", StringComparison.CurrentCultureIgnoreCase) || path.EndsWith(".tmap", StringComparison.CurrentCultureIgnoreCase))
				select path;
				foreach (string text in enumerable)
				{
					string localPath = System.IO.Path.Combine(Main.PlayerPath, fileName, FileUtilities.GetFileName(text, true));
					FileUtilities.MoveToLocal(text, localPath);
				}
				Main.CloudFavoritesData.ClearEntry(this);
				this._isCloudSave = false;
				this._path = playerPathFromName;
				Main.LocalFavoriteData.SaveFavorite(this);
			}
		}

		// Token: 0x06001E6F RID: 7791 RVA: 0x00015A12 File Offset: 0x00013C12
		public void UpdatePlayTimer()
		{
			if (Main.instance.IsActive && !Main.gamePaused && Main.hasFocus && this._isTimerActive)
			{
				this.StartPlayTimer();
				return;
			}
			this.PausePlayTimer();
		}

		// Token: 0x06001E70 RID: 7792 RVA: 0x00015A43 File Offset: 0x00013C43
		public void StartPlayTimer()
		{
			this._isTimerActive = true;
			if (!this._timer.IsRunning)
			{
				this._timer.Start();
			}
		}

		// Token: 0x06001E71 RID: 7793 RVA: 0x00015A64 File Offset: 0x00013C64
		public void PausePlayTimer()
		{
			if (this._timer.IsRunning)
			{
				this._timer.Stop();
			}
		}

		// Token: 0x06001E72 RID: 7794 RVA: 0x00015A7E File Offset: 0x00013C7E
		public TimeSpan GetPlayTime()
		{
			if (this._timer.IsRunning)
			{
				return this._playTime + this._timer.Elapsed;
			}
			return this._playTime;
		}

		// Token: 0x06001E73 RID: 7795 RVA: 0x00015AAA File Offset: 0x00013CAA
		public void StopPlayTimer()
		{
			this._isTimerActive = false;
			if (this._timer.IsRunning)
			{
				this._playTime += this._timer.Elapsed;
				this._timer.Reset();
			}
		}

		// Token: 0x06001E74 RID: 7796 RVA: 0x00015AE7 File Offset: 0x00013CE7
		public void SetPlayTime(TimeSpan time)
		{
			this._playTime = time;
		}

		// Token: 0x0400196C RID: 6508
		private Player _player;

		// Token: 0x0400196D RID: 6509
		private TimeSpan _playTime = TimeSpan.Zero;

		// Token: 0x0400196E RID: 6510
		private Stopwatch _timer = new Stopwatch();

		// Token: 0x0400196F RID: 6511
		private bool _isTimerActive;

		// Token: 0x04001970 RID: 6512
		public CustomModDataException customDataFail;
	}
}
